package com.wj.rspmproperty.uitls;

/**
 * @author WeiJin
 * @version 1.0
 */
public class StringUtil {
    private StringUtil(){}

    /**
     * 数字转换成长度为2的字符串
     * @param i
     * @return
     */
    public static String numberToLen2String(int i) {
        if (-1 < i && i < 10) {
            return "0" + i;
        } else {
            return i + "";
        }
    }

    /**
     * 从字符串中获取数字
     * @param s
     * @return
     */
    public static int getNumberFromString(String s) {
        char[] chars = s.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            if (c >= '0' && c <= '9') {
                builder.append(c);
            }
        }
        return Integer.parseInt(builder.toString());
    }

    /**
     * 把小数字符串价格乘以100
     * @param price
     * @return
     */
    public static Integer stringPriceToInt(String price) throws Exception{
        if(price != null){
            try{
                Double d = Double.valueOf(price);
                return Double.valueOf(d * 100).intValue();
            }catch (NumberFormatException e){
                throw new Exception("不是浮点数格式");
            }
        }
        return null;
    }
}
